package com.showcal.platform.utils;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.showcal.platform.domain.ImSignModel;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Lists;

/***
 * 需要依赖 commons-codec请自行依赖或者根据规范自行开发 *
 * 
 *  建议更改  domain 和appToken对象后使用getLoginModel 方法更方便。
 * 
 * @date 2 Apr, 2015
 * @see
 * @since
 */
public class LoginSH256Util {
    public static  String domain ; //更换成你自己的 domain
    public static  String appToken  ; //更换成你自己的appToken
    
    
    
    /***
     * 根据appKey，appSecret 获取 登陆的对象。
    * @param appKey
    * @param appSecret
    * @param openId
    * @return
     */
     public static ImSignModel getLoginModel(String appKey,
             String appSecret, Long openId) {
         String nonce = RandomStringUtils.randomAlphanumeric(6);
         Long timestamp = System.currentTimeMillis() / 1000;
         String signature = getSignature(appToken, appSecret, openId, nonce, timestamp);
         return new ImSignModel(domain, appKey, openId, nonce, timestamp, signature);
     }

    /***
     * 获取签名
     * 
    * @desc 
    * @param appToken 须从backyard或悟空IM管理人员获取到
    * @param appSecret  须从backyard或悟空IM管理人员获取到
    * @param openId      用户唯一标示
    * @param nonce       随机数
    * @param timestamp 当前以秒计的时间戳
    * @return  签名
     */
    public static String getSignature(String appToken, String appSecret, long openId, String nonce,
            long timestamp) {
        List<String> list =  Lists.newArrayList(appToken, appSecret, "" + openId, nonce, "" + timestamp);
        Collections.sort(list);
        String join = StringUtils.join(list, "");
        return new String(DigestUtils.sha256Hex(join));
    }

    /**
     * 
    * @desc  检测签名是否合法
    * @param appToken
    * @param appSecret
    * @param openId
    * @param nonce
    * @param timestamp
    * @param singature
    * @return
     */
    public static boolean checkSingature(String appToken, String appSecret, long openId,
            String nonce, long timestamp, String singature) {
        String sign = getSignature(appToken, appSecret, openId, nonce, timestamp);
        return (sign == null) ? false : (sign.equals(singature));
    }

    /**
    * @desc 
     * 获取登陆对象  ，里面有各项免用户导入的数据
     * 
    * @param domain
    * @param appToken
    * @param appKey   需要是  “端”  上使用的那个appKey ,也就是说这个最好有标示出是从什么端过来的appKey。或者请求过来的参数里带这个数据
    * @param appSecret
    * @param openId
    * @return
     */
    public static ImSignModel getLoginModel(String domain, String appToken, String appKey,
            String appSecret, Long openId) {
        String nonce = RandomStringUtils.randomAlphanumeric(6);
        Long timestamp = System.currentTimeMillis() / 1000;
        String signature = getSignature(appToken, appSecret, openId, nonce, timestamp);
        return new ImSignModel(domain, appKey, openId, nonce, timestamp, signature);
    }


    public static void setDomain(String domain) {
        LoginSH256Util.domain = domain;
    }

    public static void setAppToken(String appToken) {
        LoginSH256Util.appToken = appToken;
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        domain = "showcal";
        appToken = "SPoEHlw9zbytI068GJ0OMKWYQpjWiDjP";
        String appkey = "ce2a020e4f400c0133cf91d76163685f";
        String appSecret = "S669mLAgOy1irlo60Ik96hRDWBoom6xj";
        Long openId = 646918257127067648L;
        String nonce = RandomStringUtils.randomAlphanumeric(6);
        Long timestamp = System.currentTimeMillis() / 1000;
//        ImSignModel imSignModel =  LoginSH256Util.getLoginModel(domain,appToken,appkey, appSecret,openId);

        String signature  = LoginSH256Util.getSignature(appToken,appSecret,openId,nonce,timestamp);
        ImSignModel imSignModel = new  ImSignModel(domain, appkey, openId, nonce, timestamp, signature);

        System.out.println(JSON.toJSONString(imSignModel));
//        Boolean issuccess = checkSingature(appToken,appSecret,openId,imSignModel.getNonce(),imSignModel.getTimestamp(),imSignModel.getSignature());
//        System.out.println(issuccess);
    }
}
